﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;

namespace StockMentor
{
    public partial class frmEditFurnizori : Form
    {
        private string sCif;
        DataSet dsFurnizori = new DataSet();
        DataTable dtFurnizori = new DataTable();
        SqlDataAdapter daFurnizori = new SqlDataAdapter();
        string sFurn = string.Empty;
        SqlCommand commFurn = new SqlCommand();
        public static SqlConnection connStatic;
        SqlConnection conn;
        private static Form _singletonInstance = null;
        private Form formCalling;
     
        public frmEditFurnizori(SqlConnection conn)
        {
            InitializeComponent();
            this.conn = conn;

        }

        public void SetKeyValues(string sCif, Form formCalling)
        {
            this.sCif = sCif;
            this.formCalling = formCalling;
        }

        private void btnIesire_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public static Form GetSingletonInstance(SqlConnection conection)
        {
            if (_singletonInstance == null || _singletonInstance.IsDisposed)
            {
                connStatic = conection;
                _singletonInstance = new frmEditFurnizori(connStatic);
            }
            return _singletonInstance;
        }

        private void frmEditFurnizori_Load(object sender, EventArgs e)
        {
            if (formCalling.Name.Equals("frmFurnizori"))
            {

                sFurn = "SELECT * FROM furnizori WHERE furn_cif = '" + sCif + "'";

                daFurnizori = new SqlDataAdapter(sFurn, conn);
                dsFurnizori = new DataSet();

                daFurnizori.Fill(dsFurnizori, "furnizori");

                dtFurnizori = dsFurnizori.Tables[0];

                txtDenumire.Text = dtFurnizori.Rows[0]["furn_denumire"].ToString();
                txtCif.Text = sCif;
                txtCod.Text = dtFurnizori.Rows[0]["furn_cod"].ToString();
                txtAdresa.Text = dtFurnizori.Rows[0]["furn_adresa"].ToString();
                txtBanca.Text = dtFurnizori.Rows[0]["furn_banca"].ToString();
                txtIban.Text = dtFurnizori.Rows[0]["furn_iban"].ToString();
                txtEmail.Text = dtFurnizori.Rows[0]["furn_email"].ToString();
                txtRegCom.Text = dtFurnizori.Rows[0]["furn_regc"].ToString();
            }
            else if (formCalling.Name.Equals("frmReceptii"))
            {
                txtCif.Enabled = true;
                btnSterge.Visible = false;
                lblInfo.Text = "Ati ales sa adaugati datele urmatorului furnizor:";
                txtDenumire.Text = txtCod.Text = txtCif.Text = txtRegCom.Text = txtBanca.Text = txtEmail.Text = txtAdresa.Text = txtIban.Text = string.Empty;
            
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            switch (MessageBox.Show("Doriti sa efectuati salvarea furnizorului ?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
            {
                case DialogResult.Yes:
                    break;
                case DialogResult.No:
                    return;
                    break;
            }
            try
            {
                string sMsg = string.Empty;

                if (txtDenumire.Text.Trim().Length == 0)
                {
                    sMsg = " Denumire ";
                    goto msgErr;
                }

                if (txtCif.Text.Trim().Length == 0)
                {
                    sMsg = " CIF ";
                    goto msgErr;
                }
                if (txtRegCom.Text.Trim().Length == 0)
                {
                    sMsg = " Registrul comertului ";
                    goto msgErr;
                }
                if (txtAdresa.Text.Trim().Length == 0)
                {
                    sMsg = " Adresa ";
                    goto msgErr;
                }
                CustomProcedures.conn = connStatic;
                if (formCalling.Name.Equals("frmReceptii"))
                {
                    CustomProcedures.INSERTFURNIZORI(txtCif.Text,
                                                     txtRegCom.Text,
                                                     txtDenumire.Text,
                                                     txtAdresa.Text,
                                                     txtCod.Text,
                                                     txtIban.Text,
                                                     txtBanca.Text,
                                                     txtEmail.Text,
                                                     System.DateTime.Today 
                                                     );
                    MessageBox.Show("Salvarea furnizorului s-a efectuat cu succes!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    ((frmReceptii)formCalling).CallBack(txtDenumire.Text);
           
                }
                else if (formCalling.Name.Equals("frmFurnizori"))
                {
                    
                    CustomProcedures.UPDATEFURNIZORI(txtRegCom.Text,
                                                     txtDenumire.Text,
                                                     txtAdresa.Text,
                                                     txtCod.Text,
                                                     txtIban.Text,
                                                     txtBanca.Text,
                                                     txtEmail.Text,
                                                     txtCif.Text
                                                     );
                    MessageBox.Show("Salvarea furnizorului s-a efectuat cu succes!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    ((frmFurnizori)formCalling).CallBack();
                }
                this.Close();
                return;
            msgErr: MessageBox.Show("Pentru adaugarea unui furnizor, campul" + sMsg + "trebuie completat!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            catch (Exception ex)
            {
                MessageBox.Show("CIF furnizor deja definit!", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
                    
        }

           private void btnSterge_Click(object sender, EventArgs e)
        {
            switch (MessageBox.Show("Doriti sa efectuati stergerea furnizorului?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
            { 
                case DialogResult.Yes:
                    break;
                case DialogResult.No:
                    return;
                    break;
            }
           
            try
            {
                sFurn = "DELETE FROM furnizori WHERE furn_cif = '" + sCif +"'";

                commFurn = new SqlCommand(sFurn, conn);


                SqlDataReader readerFurn = commFurn.ExecuteReader();

                readerFurn.Dispose();
                readerFurn.Close();
                txtDenumire.Text = txtCod.Text = txtCif.Text = txtRegCom.Text = txtBanca.Text = txtEmail.Text = txtAdresa.Text = txtIban.Text = string.Empty;

                MessageBox.Show("Stergerea furnizorului s-a efectuat cu succes!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                ((frmFurnizori)formCalling).CallBack();
                
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Stergerea furnizorului nu s-a efectuat. S-au intampinat erori la stergere!", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }
    }
}
